package com.topone.service.impl;

import com.topone.Utils.ServiceUtils;
import com.topone.dataObject.TcUser;
import com.topone.service.TcUserService;
import com.topone.service.WechatService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;

@Service
public class WechatServiceImpl implements WechatService {

    @Autowired
    private TcUserService userService;

    @Override
    public void saveWechat(WxMpUser user) {
        TcUser temUser = null;
        if (Objects.nonNull(user) && StringUtils.isNotEmpty(user.getOpenId())) {
            temUser = userService.findByopenid(user.getOpenId());
        }
        if (Objects.isNull(temUser)) {
            temUser = ServiceUtils.toWechat(user);
            temUser.setUserId(UUID.randomUUID().toString().replace("-", ""));
            userService.save(temUser);
        } else {
            TcUser toUser = ServiceUtils.toWechat(user);
            toUser.setUserId(temUser.getUserId());
            toUser.setHandPhone(temUser.getHandPhone());
            userService.save(toUser);
        }
    }
}
